using System;
using System.Collections.Generic;
using System.Html;
using Nsb;
using Nsb.Classes;
using Yoshi.Classes;
using YoshiBase.Classes;
using jQueryApi;

namespace Yoshi.Fb
{
    public sealed class TestPage : YoshiPage
    {
        public TestPage(jQueryObject element) : base(element)
        {
            //Page = this;
            Build();
        }

        public override string Title
        {
            get { return "YoshiMate' (" + Window.Location.Hostname + ")"; }
        }

        public override void Build()
        {
            base.Build();
            jQuery.Select("body").Prepend(
                jQuery.FromHtml("<div id='toolbar' class='testArea' style='position:absolute;top:0;right:0'/>").Append(
                    jQuery.FromHtml("<ul id='toollist'/>")));
            jQuery.Select("#toolbar").Prepend(
                jQuery.FromHtml("<div style='font-size:12pt; padding-left:10px'/>").Html(Date.Now.ToString()));
            jQuery.Select("body").Prepend(jQuery.FromHtml("<h1/>").Html("Yoshi Mat&eacute;"));
            AddToolLink("unit tests", "/unittests.html");
            if (!new RegularExpression("snalp.org", "i").Test(Window.Location.Hostname))
                AddToolLink("@snalp.org", "http://snalp.org");
            else
                AddToolLink("@localhost", "http://localhost/yoshi");

            new Await()
                .AddDl(Data.AuthenticateAw)
                .AddDl(
                    delegate(Await aw)
                    {
                        AddToolLink("fb.photo.php",
                                    String.Format("php/fb/fb.photo.php?access_token={0}&id={1}", Data.Auth.access_token, Data.User.id));
                        AddToolButton("get yoshi1.gif",
                                      delegate
                                      {
                                          //ui.PostDataImage();

                                          //DataImage.GetImage(
                                          //    delegate (object gif)
                                          //        {
                                          //            Inform.Log("gif content {0}", DataImage.ExtractData((string)gif));
                                          //            ui.PostToFb(gif);
                                          //        }
                                          //    );
                                      }
                            );
                        AddToolButton("Refresh",
                                      delegate
                                      {
                                          Refresh();
                                      }
                            );
                        aw.Done();
                    }
                )
                .Commit();
        }

        public override void Refresh()
        {
            base.Refresh();
            new Await()
                .AddFn(Clear)
                .AddDl(
                    delegate(Await aw)
                    {
                        Data.PhotoTag.AppendTo(Element);
                        ShowMe();
                        ShowFriends();
                        ShowAlbum();
                    })
                .Commit()
                ;
        }

        public override void UpdatePage(PageUpdate pu)
        {
            throw Exceptions.NotImplementedException;
        }

        private void ShowFriends()
        {
            AddArea("ShowFriends",
                    delegate(jQueryObject jo)
                    {
                        FbDataStore fb = (FbDataStore)Data;
                        fb.RetrieveFriends(
                            delegate(object fbo)
                            {
                                FbList[] friends = (FbList[])((FbResult)fbo).data;
                                string ot = "";
                                WriteJo(jo, String.Format("<h3>Friends ({0})</h3>", friends.Length));
                                foreach (FbList friend in friends)
                                {
                                    ot += friend.name + "; ";
                                }
                                WriteJo(jo, ot);
                            });
                    }
                );
        }

        private void ShowAlbum()
        {
            Sleep.DoNow(
                delegate
                {
                    AddArea("ShowAlbum",
                            delegate(jQueryObject jo)
                            {
                                FbDataStore fb = (FbDataStore)Data;
                                fb.RetrievePhotos("Profile Pictures",
                                                  delegate(object fbo)
                                                  {
                                                      FbPhoto[] photos = (FbPhoto[])fbo;
                                                      WriteJo(jo, String.Format("<h3>Photos</h3>"));
                                                      for (int i = 0; i < 8; i++)
                                                      {
                                                          int idx = Math.Floor(Math.Random() * photos.Length);
                                                          FbPhoto photo = photos[idx];
                                                          jQuery.FromHtml("<img width='130' style='border: 1px solid black;margin:3px' title='" + idx + "'/>").Attribute("src", photo.picture).AppendTo(jo);
                                                          if (Script.IsValue(photo.name))
                                                              Inform.Log("photo name size = {0}", photo.name.Length);
                                                      }
                                                  }
                                );
                            }
                        );
                }, 2000);
        }

        private void ShowMe()
        {
            AddArea("ShowMe",
                    delegate(jQueryObject jo)
                    {
                        string ot = "";
                        Dictionary<string, object> d =
                            (Dictionary<string, object>)(object)Data.User;
                        WriteJo(jo, String.Format("<h3>Me</h3>"));
                        foreach (string n in d.Keys)
                        {
                            ot += String.Format(
                                "<b>{0}</b>: <span class='testValue'>{1}</span><br>", n, d[n]);
                        }
                        WriteJo(jo, ot);
                    }
                );
        }
    }
}